import {Component, Input, OnInit} from '@angular/core';
import {Hero} from "../mock-heroes";
import {HeroServiceService} from "../hero-service.service";
import {ActivatedRoute, Params} from "@angular/router";
import 'rxjs/add/operator/switchMap';
import {Location} from '@angular/common';

@Component({
  selector: 'app-hero-detail',
  templateUrl: './hero-detail.component.html',
  styleUrls: ['./hero-detail.component.css'],
  providers:[HeroServiceService]
})
export class HeroDetailComponent implements OnInit {
  hero:Hero;

  constructor(private heroService: HeroServiceService,
              private router: ActivatedRoute, private location: Location) {
  }

  ngOnInit() {
    this.router.params.switchMap((params:Params)=> this.heroService.getHero(+params['id'])).subscribe(hero=>this.hero = hero);
  }

  goBack(): void {
    this.location.back();
  }

}
